using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Windows;
using System.Windows.Markup;

namespace TimersXP
{
	public partial class App : Application
	{
        ////***************************************************************************************************
        ///// <summary>Applies the skin.</summary>
        ///// <param name="skinDictionaryUri">The skin dictionary URI.</param>
        //public void ApplySkin(Uri skinDictionaryUri)
        //{
        //    //***************************************************************************************************
        //    // Load the ResourceDictionary into memory.
        //    ResourceDictionary skinDict = Application.LoadComponent(skinDictionaryUri) as ResourceDictionary;

        //    Collection<ResourceDictionary> mergedDictionaries = base.Resources.MergedDictionaries;

        //    // Remove the existing skin dictionary, if one exists.
        //    // NOTE: In a real application, this logic might need
        //    // to be more complex, because there might be dictionaries
        //    // which should not be removed.
        //    if (mergedDictionaries.Count > 0)
        //        mergedDictionaries.Clear();

        //    // Apply the selected skin so that all elements in the
        //    // application will honor the new look and feel.
        //    mergedDictionaries.Add(skinDict);
        //    //***************************************************************************************************
        //}
        ////***************************************************************************************************

        public void ApplySkin(string skinFileName, string baseSkinFileName, string skinsPath)
        {
            ResourceDictionary baseSkinDictionary = null;
            ResourceDictionary skinDictionary = null;

            string baseSkinFilePath = skinsPath + baseSkinFileName + ".xaml";
            string skinFilePath = skinsPath + skinFileName + ".xaml";

            if (File.Exists(baseSkinFilePath))
            {
                using (FileStream baseSkinFileStream = new FileStream(baseSkinFilePath, FileMode.Open))
                {
                    // Read in Base ResourceDictionary File
                    baseSkinDictionary = (ResourceDictionary)XamlReader.Load(baseSkinFileStream);
                    baseSkinFileStream.Close();
                }
            }
            else
                MessageBox.Show("File: " + baseSkinFileName + "does not exist. Please re-enter the file name.");

            // Clear any previous dictionaries loaded
            Resources.MergedDictionaries.Clear();
            if (baseSkinDictionary != null)
            {
                // Now Add the Base Skin Dictionary so that when we load the primary skin it won't throw any exceptions for dependency resource definitions.
                // We will remove it and re-merge the base skin with the primary skin before loading anyways!
                Resources.MergedDictionaries.Add(baseSkinDictionary);
            }

            if (File.Exists(skinFilePath))
            {
                using (FileStream fileStream = new FileStream(skinFilePath, FileMode.Open))
                {
                    // Read in ResourceDictionary File
                    skinDictionary = (ResourceDictionary)XamlReader.Load(fileStream);
                    fileStream.Close();
                }
            }
            else
                MessageBox.Show("File: " + skinFileName + "does not exist. Please re-enter the file name.");

            // Clear any previous dictionaries loaded
            Resources.MergedDictionaries.Clear();
            // Add in newly loaded Resource Dictionary
            if (baseSkinDictionary != null && skinDictionary != null)
            {
                skinDictionary.MergedDictionaries.Add(baseSkinDictionary);
                Resources.MergedDictionaries.Add(skinDictionary);
            }
            baseSkinDictionary = null;
            skinDictionary = null;
        }

        ///// <summary>Applies the skin.</summary>
        ///// <param name="fileName">Name of the file.</param>
        ///// <param name="withUriMethod">if set to <c>true</c> [with URI method].</param>
        //public void ApplySkin(string fileName, bool withUriMethod = true)
        //{
        //    if (File.Exists(fileName))
        //    {
        //        ResourceDictionary dictionary = new ResourceDictionary();
        //        System.Windows.Application.LoadComponent(dictionary, new System.Uri(fileName, System.UriKind.RelativeOrAbsolute));

        //        Collection<ResourceDictionary> mergedDictionaries = base.Resources.MergedDictionaries;

        //        // Remove the existing skin dictionary, if one exists.
        //        // NOTE: In a real application, this logic might need
        //        // to be more complex, because there might be dictionaries
        //        // which should not be removed.
        //        if (mergedDictionaries.Count > 0)
        //            mergedDictionaries.Clear();

        //        // Apply the selected skin so that all elements in the
        //        // application will honor the new look and feel.
        //        mergedDictionaries.Add(dictionary);
        //    }
        //}
	}
}